package com.rzico.basics.controller.admin;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Turntable;
import com.rzico.basics.entity.TurntableRecord;
import com.rzico.basics.service.TurntableRecordService;
import com.rzico.basics.service.TurntableService;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.SysUserService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "大转盘接口")
@RestController
@RequestMapping("/admin/turntable")
public class TurntableAdminController extends BaseController {

    @Autowired
    private TurntableService turntableService;

    @Autowired
    private TurntableRecordService turntableRecordService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询 大转盘 分页列表
     *
     * @param subject   活动标题
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param type      活动类型
     */
    @ApiOperation("分页查询大转盘")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subject", value = "活动标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "活动类型", dataType = "Integer", paramType = "query")
    })
    public CommResult<Turntable> list(String subject, Date startTime, Date endTime, Integer type, Pageable pageable) {
        Map<String, Object> params = new HashMap<>();
        params = buildSortField(params, pageable);

        if (StringUtils.isNotEmpty(subject)) {
            params.put("subject", subject);
        }

        if (startTime != null) {
            params.put("startTime", startTime);
        }

        if (endTime != null) {
            params.put("endTime", endTime);
        }

        if (type != null) {
            params.put("type", type);
        }
        params.put("deleted", false);
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Turntable> list = turntableService.selectList(params);
        PageResult<Turntable> pageResult = new PageResult<>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    @ApiOperation("查询单条大转盘")
    @GetMapping("/find/{id}")
    public CommResult<Turntable> find(@PathVariable String id) {
        Turntable result = turntableService.findById(id);
        return CommResult.success(result);
    }

    @ApiOperation("保存大转盘")
    @PostMapping("/save")
    public CommResult<Turntable> save(@RequestBody Turntable turntable) {
        SysUser current = sysUserService.getCurrent();
        if (current == null) return CommResult.error();
        turntable.setUserId(current.getId());
        Turntable save = turntableService.save(turntable);
        return CommResult.success(save);
    }

    @Log(desc = "批量删除营销活动", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除大转盘, ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Turntable> del(@PathVariable String ids) {
        int i = turntableService.delByIds(ids.split(","));
        return i > 0 ? CommResult.success() : CommResult.error();
    }

    @Log(desc = "更新大转盘", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新大转盘")
    @PostMapping("/update")
    public CommResult<Turntable> update(@RequestBody Turntable turntable) {
        Turntable update = turntableService.update(turntable);
        return CommResult.success(update);
    }

    @ApiOperation("分页查询大转盘记录")
    @GetMapping("/recordList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "turntableId", value = "大转盘ID", dataType = "Long", paramType = "query"),
    })
    public CommResult<TurntableRecord> recordList(Long turntableId, Pageable pageable) {
        Map<String, Object> params = new HashMap<>();
        params.put("turntableId", turntableId);
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<TurntableRecord> records = turntableRecordService.selectList(params);
        PageResult<TurntableRecord> pageResult = new PageResult<>(records, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

}
